const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {},

  /**
   * 组件的初始数据
   */
  data: {
    userId: 0,
    companyId: 0,
    totalNum: 0,
    employeeList: [],
    show: false,
    startTime: "",
    endTime: "",
    allChecked: false,
    minDate: new Date("2020/12/30").getTime(),
    maxDate: new Date().getTime()
  },

  attached() {
    const companyId = app.globalData.userInfo.companyData.companyId;
    const userId = app.globalData.userInfo.userDetail.id;
    this.setData({
      companyId,
      userId
    });
    this.getData();
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onPullDownRefresh() {
      this.getData();
    },
    onReachBottom() {
      // this.getData()
    },
    getData() {
      const { userId, startTime, endTime, companyId } = this.data;
      let s = startTime ? wx.$dayjs(startTime).format("YYYY-MM-DD") + " 00:00:00" : "";
      let e = endTime ? wx.$dayjs(endTime).format("YYYY-MM-DD") + " 23:59:59" : "";
      wx.$apis
        .getNonSettlementOrder({
          userId,
          startTime: s,
          endTime: e,
          companyId,
          asc: true,
          columns: [],
          current: 1,
          currentMongo: 0,
          size: 100
        })
        .then((res) => {
          wx.stopPullDownRefresh();
          if (res.data) {
            let totalNum = 0;
            const { records } = res.data;
            let employeeList = records.map((item) => {
              totalNum += item.count * 1;
              return {
                ...item,
                createTime: item.createTime ? wx.$dayjs(item.createTime).format("YYYY-MM-DD") : ""
              };
            });
            this.setData({
              employeeList,
              totalNum
            });
          }
        });
    },
    onConfirm(event) {
      const [start, end] = event.detail;
      this.setData({
        show: false,
        startTime: this.formatDate(start),
        endTime: this.formatDate(end)
      });
      this.getData();
    },
    formatDate(date = new Date()) {
      return wx.$dayjs(date).format("YYYY/MM/DD");
    },
    onDisplay() {
      this.setData({ show: true });
    },
    onClose() {
      this.setData({ show: false });
    }
  }
});
